package com.kedou.app;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kedou.adapter.ProlistListAdapter;
import com.kedou.domain.ProjectItem;
import com.kedou.net.PostProjectList;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;
import com.kedou.util.DateUtil;
import com.kedou.widget.PullListView;
import com.kedou.widget.PullListView.IXListViewListener;

/**
 * 项目列表 界面
 * 
 * @author zhouchao
 * 
 */
public class ProjectListActivity extends BaseActivity implements
		OnClickListener, IXListViewListener, OnItemClickListener {
	/**
	 * 顶部左侧 扫描
	 */
	private ImageView mImgSao;
	/**
	 * 顶部右侧 设置
	 */
	private ImageView mImgSetting;
	/**
	 * 详细情况
	 */
	private RelativeLayout mRlDesc;
	/**
	 * 下拉刷新，上拉加载 listview
	 */
	private PullListView mPullListview;
	/**
	 * 项目列表
	 */
	private ArrayList<ProjectItem> mProjects;
	/**
	 * 适配器
	 */
	private ProlistListAdapter mAdapter;
	private int page = 1;
	private String time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_projectlist);
		initView();
		setListener();
		mProjects = new ArrayList<ProjectItem>();
		mAdapter = new ProlistListAdapter(this, mProjects);
		mPullListview.setAdapter(mAdapter);
		time = DateUtil.getStringDate1();
		if (NetworkUtil.isNetworkAvailable(this)) {
			getProjectList();
		} else {
			Toast.makeText(
					this,
					getResources()
							.getString(R.string.please_connect_to_network),
					Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mImgSao = (ImageView) findViewById(R.id.img_sao);
		mImgSao.setVisibility(View.VISIBLE);
		mImgSetting = (ImageView) findViewById(R.id.img_setting);
		mImgSetting.setVisibility(View.VISIBLE);
		mPullListview = (PullListView) findViewById(R.id.listview);
		mPullListview.setPullLoadEnable(true);
		mPullListview.setPullRefreshEnable(true);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mImgSao.setOnClickListener(this);
		mImgSetting.setOnClickListener(this);
		mPullListview.setXListViewListener(this);
		mPullListview.setOnItemClickListener(this);
	}

	/**
	 * 网络获取列表
	 */
	private void getProjectList() {
		mProgressDialog.show();
		if (NetworkUtil.isNetworkAvailable(ProjectListActivity.this)) {
			new PostProjectList(PreferencesUtil.getString(this, "token"),
					PreferencesUtil.getString(this, "userId"), 1,
					Config.SOURCE, Util.getImei(this),
					new PostProjectList.SuccessCallback() {

						@Override
						public void onSuccess(String msg,
								ArrayList<ProjectItem> projects) {
							if (projects != null) {
								mProgressDialog.dismiss();
								mAdapter.addList(projects, true);
							}

						}
					}, new PostProjectList.FailCallback() {

						@Override
						public void onFail(String msg) {
							mProgressDialog.dismiss();
							Toast.makeText(ProjectListActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, ProjectListActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_sao:

			break;
		case R.id.img_setting:

			break;

		}
	}

	/**
	 * 下拉刷新
	 */
	@Override
	public void onRefresh() {
		if (NetworkUtil.isNetworkAvailable(ProjectListActivity.this)) {
			new PostProjectList(PreferencesUtil.getString(
					ProjectListActivity.this, "token"),
					PreferencesUtil.getString(ProjectListActivity.this,
							"userId"), 1, Config.SOURCE,
					Util.getImei(ProjectListActivity.this),
					new PostProjectList.SuccessCallback() {

						@Override
						public void onSuccess(String msg,
								ArrayList<ProjectItem> projects) {
							if (projects != null) {
								mAdapter.addList(projects, true);
								mPullListview.stopRefresh();
								mPullListview.setRefreshTime(time);
								time = DateUtil.getStringDate1();
							}

						}
					}, new PostProjectList.FailCallback() {

						@Override
						public void onFail(String msg) {
							Toast.makeText(ProjectListActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, ProjectListActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * 上拉加载
	 */
	@Override
	public void onLoadMore() {
		page++;
		if (NetworkUtil.isNetworkAvailable(ProjectListActivity.this)) {
			new PostProjectList(PreferencesUtil.getString(
					ProjectListActivity.this, "token"),
					PreferencesUtil.getString(ProjectListActivity.this,
							"userId"), page, Config.SOURCE,
					Util.getImei(ProjectListActivity.this),
					new PostProjectList.SuccessCallback() {

						@Override
						public void onSuccess(String msg,
								ArrayList<ProjectItem> projects) {
							if (projects != null) {
								mAdapter.addList(projects, false);
							}

						}
					}, new PostProjectList.FailCallback() {

						@Override
						public void onFail(String msg) {
							Toast.makeText(ProjectListActivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, ProjectListActivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * PullListview的item点击事件
	 * 
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ProjectItem projectItem = mAdapter.getList().get(arg2 - 1);
		Intent intent = new Intent(ProjectListActivity.this,
				ProjectDetailActivity.class);
		Bundle bundle = new Bundle();
		bundle.putParcelable("project_item", projectItem);
		intent.putExtra("project", bundle);
		ProjectListActivity.this.startActivity(intent);
	}
}
